#ifndef HEADER_FILE
#define HEADER_FILE
#include <string.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#define MAXSIZE 1024
typedef int elementype;
char *help = "https://www.runoob.com/cprogramming/c-examples.html";

/*
十进制转换二进制
*/
char *to_binary(long num, char result[])
{

    int r = num % 2;
    if (num > 1)
    {
        to_binary(num / 2, result);
        strcat(result, r == 0 ? "0" : "1");
    }
    if (num == 1)
    {
        strcat(result, "1");
    }

    return result;
}

// 复制数组 int arr[]
int intcpy(int *copied, int *original, int length, int print_flag)
{
    int loop;
    for ( loop = 0; loop < length; loop++)
    {
        copied[loop] = original[loop];
    }

    if (print_flag != 0)
    {
        printf("元素数组 -> 拷贝后的数组 \n");
        for (loop = 0; loop < length; loop++)
        {
            printf("   %2d        %2d\n", original[loop], copied[loop]);
        }
    }
}

// 打印 int arr[]
int printarr(int *arr, int length)
{
    if (sizeof(arr) == 0)
    {
        return 0;
    }
    size_t i;
    for (i = 0; i < length; i++)
    {
        printf("%d%c", arr[i], 32);
    }

    return 1;
}

// 字符串反转
void reverse(char *s)
{
    // 获取字符串长度
    int len = 0;
    char *p = s;
    while (*p != 0)
    {
        len++;
        p++;
    }

    // 交换 ...
    int i = 0;
    char c;
    while (i <= len / 2 - 1)
    {
        c = *(s + i);
        *(s + i) = *(s + len - 1 - i);
        *(s + len - 1 - i) = c;
        i++;
    }
}

// char msg[]转ASCII的int []
void strConvertAscii(char *str, int *arr)
{
    size_t i;
    for ( i = 0; i < strlen(str); i++)
    {
        arr[i] = str[i];
    }
}

// int num[] 转char msg[]
void intConvertString(int *arr, int length, char *msg)
{
    size_t i;
    for ( i = 0; i < length; i++)
    {
        sprintf(msg + i, "%s", arr + i);
    }
}


// 读取多行文件,返回char 长度
int loadFile(char *filepath, char *buffer)
{
    FILE *pf;
    int c;

    if ((pf = fopen(filepath, "r")) == NULL)
    {
        printf("打开文件失败！\n");
        exit(1);
    }

    int i = 0;
    while (1)
    {
        
        c = fgetc(pf);
        if (feof(pf))
        {
            buffer[i] = 0;
            break;
        }
        buffer[i] = c;
        i++;
        
    }

    fclose(pf);
    return i;
}

void  fileConvertArr(char *filepath,char*msg,int *arr)
{
    char *data;
    int length = loadFile(filepath, msg);
    strConvertAscii(msg, arr);

    for (size_t i = 0; i < length; i++)
    {
        if (i == 0)
        {
            printf("int arr[] = {%d,", arr[i]);
        }

        else if (i == length - 1)
        {
            printf("%d", arr[i]);
            printf("};");
        }
        else
        {
            printf("%d,", arr[i]);
        }
    }
    putchar('\n');
    printf("length:%d\n",length);
}
#endif